﻿using System.Collections;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.ComponentModel;

namespace SiteFox.WebParts.Multimedia
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    
    public class ImageRotatorWP : WebPart
    {
        Literal _lit_Images;

        #region Properties
        private string _FolderUrl = "";
        [Personalizable(true)]
        [WebBrowsable(true)]
        [WebDescription("Enter folder relative path (~/FolderName).")]
        [WebDisplayName("FolderUrl")]
        public string FolderUrl
        {
            get { return _FolderUrl; }
            set { _FolderUrl = value; }
        }

        private int _FrameWidth = 120;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Width (px).")]
        [WebDisplayName("Width (px)")]
        public int FrameWidth
        {
            get { return _FrameWidth; }
            set { _FrameWidth = value; }
        }

        private int _FrameHeight = 200;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Height (px).")]
        [WebDisplayName("Height (px)")]
        public int FrameHeight
        {
            get { return _FrameHeight; }
            set { _FrameHeight = value; }
        }

        private bool _AllowJPG = true;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Show images with JPG extensions.")]
        [WebDisplayName("Allow JPG")]
        public bool AllowJPG
        {
            get { return _AllowJPG; }
            set { _AllowJPG = value; }
        }

        private bool _AllowGIF = true;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Show images with GIF extensions.")]
        [WebDisplayName("Allow GIF")]
        public bool AllowGIF
        {
            get { return _AllowGIF; }
            set { _AllowGIF = value; }
        }

        private bool _AllowPNG = true;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Show images with PNG extensions.")]
        [WebDisplayName("Allow PNG")]
        public bool AllowPNG
        {
            get { return _AllowPNG; }
            set { _AllowPNG = value; }
        }

        private bool _AllowBMP = true;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Show images with BMP extensions.")]
        [WebDisplayName("Allow BMP")]
        public bool AllowBMP
        {
            get { return _AllowBMP; }
            set { _AllowBMP = value; }
        }

        public enum Effects { Fade, Slide, Horizontal };
        private Effects _EffectsType = Effects.Fade;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Select effect.")]
        [WebDisplayName("Effects")]
        public Effects EffectsType
        {
            get { return _EffectsType; }
            set { _EffectsType = value; }
        }

        private int _Interval = 3;
        [Personalizable()]
        [WebBrowsable(true)]
        [WebDescription("Set SlideShow interval in seconds.")]
        [WebDisplayName("Interval")]
        public int Interval
        {
            get { return _Interval; }
            set { _Interval = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            _lit_Images = new Literal();
            _lit_Images.Text = ReturnImages();
            Controls.Add(_lit_Images);
        }

        string ReturnImages() 
        { 
            string _Images = "<div style=\"height:"+_FrameHeight+"px; width:"+FrameWidth+"px;\" class=\"ImageRotatorWP_Images_"+ this.ID +"\">";
            string _ImagesTabs = "<div style=\"display: none;\" class=\"ImageRotatorWP_Tabs_" + this.ID + "\">";
            string _Script = "";
            int _NumberOfImages = 0;

            if (_FolderUrl != "") 
            {
                string _FolderPath = CorrectVirtualPath(_FolderUrl);
                bool _FolderExists = false;
                try 
                { 
                    _FolderExists = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + _FolderPath)).Exists;
                }
                catch { }
                if(_FolderExists)
                {
                    DirectoryInfo _Dir = new DirectoryInfo(HttpContext.Current.Server.MapPath("~" + _FolderPath));
                    FileInfo[] _Files = _Dir.GetFiles();
                    for (int i = 0; i < _Files.Length; i++)
                    {
                        FileInfo _File = _Files[i];
                        string _FileExtension = _File.Extension.Substring(1);
                        if ((AllowBMP && _FileExtension == "bmp") || (AllowJPG && _FileExtension == "jpg") || (AllowPNG && _FileExtension == "png") || (AllowGIF && _FileExtension == "gif")) 
                        { 
                            _Images += "<img style=\"display: none; position: absolute;\" height='" + _FrameHeight + "px' width='" + _FrameWidth + "px' src='" + this.AppPath() + _FolderPath + "/" + _File.Name + "'>";
                            _ImagesTabs += "<a href=\"#\"></a>";
                            _NumberOfImages++;
                        }
                    }
                }
                if (_NumberOfImages > 0) 
                { 
                    _Script = "<script type=\"text/javascript\">" +
                                "$(\".ImageRotatorWP_Tabs_" + this.ID + "\").tabs(\".ImageRotatorWP_Images_" + this.ID + " > img\", {" +
                                    "effect: '" + _EffectsType.ToString().ToLower() + "'," +
                                    "fadeOutSpeed: \"slow\"," +
                                    "rotate: true" +
                                "}).slideshow({interval: " + _Interval.ToString() + "000});" +
                                "$(\".ImageRotatorWP_Tabs_" + this.ID + "\").data(\"slideshow\").play();" +
                            "</script>";
                }
            }
            _Images += "</div>";
            _ImagesTabs += "</div>";
            return _Images + _ImagesTabs + _Script;
        }
        string CorrectVirtualPath(string _VirtualPath) 
        {
            //LastChar correction
            string _LastChar = _VirtualPath.Substring(_VirtualPath.Length - 1, 1);
            if (_LastChar == "/") 
            {
                _VirtualPath = _VirtualPath.Substring(0, _VirtualPath.Length - 1);
            }
            //FirstChar correction
            string _FirstChar = _VirtualPath.Substring(0, 1);
            if (_FirstChar == "~")
            {
                _VirtualPath = _VirtualPath.Substring(1, _VirtualPath.Length - 1);
            }
            //Return corrected virtualPath
            return _VirtualPath;
        }
        string AppPath() 
        { 
            string _AppPath = HttpContext.Current.Request.ApplicationPath;
            if (_AppPath == "/")
            {
                _AppPath = "";
            }
            return _AppPath;
        }
    }
}
